package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.FriendsListAdapter;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.InviteFriendListener;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.ViewHolder;
import com.yksj.healthtalk.utils.DataParseUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 通过名字邀请好友
 * @author zhao
 */
public class InviteByNameFragment extends Fragment implements OnClickListener,InviteFriendListener{
	
	ListView mListView;
	EditText mEditText;
	FriendsListAdapter mAdapter;
	
	final ObjectHttpResponseHandler mHandler = new ObjectHttpResponseHandler(getActivity()) {
		@Override
		public Object onParseResponse(String cotent) {
			return DataParseUtil.parseInviteResultData(cotent);
		}
		public void onSuccess(int statusCode, Object response) {
			if(response instanceof String){
				//邀请错误
				ToastUtil.showShort(getActivity(), String.valueOf(response));
			}else{
				mAdapter.onDataChange((List<CustomerInfoEntity>)response);
			}
		};
		public void onFinish() {
			mPullToRefreshListView.onRefreshComplete();
		};
	};
	private int pageSize = 1;
	private int pageNum = 20;
	private PullToRefreshListView mPullToRefreshListView; 
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.invite_friend_by_name_layout,null);
		mPullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.listview);
		mPullToRefreshListView.setRefreshing();
		mListView = mPullToRefreshListView.getRefreshableView();
		mEditText = (EditText)view.findViewById(R.id.search_edit);
		view.findViewById(R.id.search_btn).setOnClickListener(this);
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ViewHolder holder = (ViewHolder)view.getTag();
				boolean  isChecked = holder.chooseBox.isChecked();
				CustomerInfoEntity entity = mAdapter.getItem(position-1);
				String userid = entity.getId();
				mAdapter.onChoose(userid,!isChecked);
				holder.chooseBox.setChecked(!isChecked);
			}
		});
		
		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				onQueryData(++pageSize,pageNum);
			}
		});
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new FriendsListAdapter(getLayoutInflater(savedInstanceState),getActivity());
		mListView.setAdapter(mAdapter);
	}
	
	/**
	 * 按名字查询
	 */
	private void onQueryData(int pageSize,int  pageNum){
		mAdapter.entities.clear();
		mAdapter.notifyDataSetChanged();
		
		String name = mEditText.getText().toString();
		mEditText.setText(null);
		
		if (SmartFoxClient.getLoginUserId()== null ) {
			return;
		}
		
		HttpRestClient.doHttpInviteByName(SmartFoxClient.getLoginUserId(), name, pageSize, pageNum, mHandler);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.search_btn:
			onQueryData(pageSize,pageNum);
			break;
		}
	}

	@Override
	public List<String> onInvite() {
		return new ArrayList<String>(mAdapter.mChoosedMap.keySet());
	}
}
